Moved validate code to own function
Modified structure and debug code