Added state and fixed bugs