Cleaned up code, needs more comments and readability improvements