Fixed minor bugs. Cleaned up code