Refactor code and fix GUI