better implementation

use less if statements to check validity