Code cleanup

 - removed unused vars
 - gave some vars more meaningful names
 - other improvements