Simplified complex stuff and cleaned it up a bit