added guards and some comments