further improvements and cleaned up code