removed unused function and slightly reorganized code