State machine feat

@Mioa Fixed code standard issues, please review