Re-factored code to use actual state system