Bug fixes
Cleaned up source a bit