Fixed numerous problems and made more efficient.