Rewrite

Tinkering with Ohad Raz's original code:

 * Added namespace, making it PHP 5.3 minimum.
 * Changed to use PSR-2.
 * Improved consistency and readability of documentation.
 * Refactored some duplicate code out into protected methods.