Refactor! plus bugfixes @torresda