Code cleanup

Adding documentation as necessary and make the code more efficient/consistent where possible