Refactor of gui and fix bug with state checking