Bug fixes

Cleaned up the code