General code cleanup

removed unused directives, fixed a bug, used better coding practices