Cleaning up, commenting

Source file split into 2 parts: 1. main script, 2. function scripts