Fixed several bugs and added some features

Random fixes.