moved most logic to .js file