improved conditional checks and improved overall code structure