Merged Ninenium's refactor of the code from version 1.1.