structured the code, minor changes