Merge CVanorman's code into zonyl's

Never have attempted this before. Hopefully it works.