refactoring for easier code, bug fixes