Refactored and cleaned up unused code. Added logging where missing