fixed logic and added states