fix tribial bugs and cleanup