some corrections etc., still doesn't work proparly though something wrong with apply