refactor code and add UI functionality