Fixing bugs and cleanup