more thorough implementation and testing

more thorough implementation and testing