Improved version

Refactored by @gigamonkey