moved things around a bit and some errorhandling