Lots and lots of fixes, additions, optimizations... prolly should've committed this piece by piece!