Refactor GUI code and fix some minor issues