Fixed some warning, refactored state