fixed if stmt and refactored