Workaround for PHP Bug (array) type juggling. Some small refactorings and cleanup