Change to use functions, add test code