Refactor. Also adjusted coding style to match WP (minus silly Yoda conditions)