Merged in #158 manually due to massive merge conflicts. Closes #158