Fix bugs and cleanup