codes form lecture with some modifications
ignore