Clean up code a bit and add doc string