works but needs check for bugs