Updated to include tests and fix functionality