Code cleanup and formatting, added documentation