Merged latest changes. Cleaned up code a little bit.