Cleaned up and optimized code

Value verification is now handled by a separate function that returns a
boolean and that consists of a crazy huge if statement.