Cleaned up code, comments will be added soon