revised final code NOT TESTED