Bugfix - typo in variable name
Code cleanup and reorganization