code cleanup, and adding new functions