Support for Collections

Added support for Collections, and did some minor refactoring. Please review.