Eliminate junk and do some refactoring