Cleaned up and made more stable