Clean up, make code more generic