fix bugs and cleanup