fixed some loop and conditional stuff