final logic before adding values
final working version