added error checking
final but buggy