Cleaned up code after frantic mess