cleanup code
change while to for loop
rename some macros to more suitable name