Some bugfixes and changes

Please feel free to ask if something needs explanation...