tiry up code a bit, add comments