Cleaned up the code a bit, added comment blocks, etc.