Fixed various issues

This is the finalized version