Reorganized functions, removed some commented code