Cleaned up the source code considerably