Bugfixes2

Cleaned up my patches slightly (merged one and added a missing close(handle)), this should make the merge easier.