[IMP] [FIX] Cleaned code. Also fixed regression bugs introduced by simplifying loops...