Ported exisiting code from old repository (after cleaning it a bit)