Updated and cleaned up to use contemporary APIs