rewritten the code, removed unnecessary stuff