Fix bugs, remove unused code